package com.commodity.Dao.inventoryDaoImp;

import com.commodity.Dao.InventoryDao;
import com.commodity.common.Global;
import com.commodity.entity.Inventory;
import com.commodity.entity.InventoryData;
import com.commodity.entity.InventorySearchBean;
import com.commodity.utils.PaginateInfo;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class InventoryDaoImp implements InventoryDao {
    private final JdbcTemplate jdbcTemplate = new JdbcTemplate(Global.getDataSource());
    private final RowMapper<Inventory> MAPPER = new BeanPropertyRowMapper<>(Inventory.class);
    private final RowMapper<InventoryData> MAPPER1 = new BeanPropertyRowMapper<>(InventoryData.class);

    @Override
    public List<Inventory> findAll(InventorySearchBean isb, PaginateInfo pi) {
        String countSql = "select count(0) from t_inventory";
        StringBuilder sb = new StringBuilder();
        List<Object> args = new ArrayList<>();

        if (StringUtils.hasText(isb.getWarehouseId())) {
            sb.append(" and warehouse_id = ?");
            args.add(isb.getWarehouseId());
        }
        if (StringUtils.hasText(isb.getCommodityId())) {
            sb.append(" and commodity_id = ?");
            args.add(isb.getCommodityId());
        }
        if (isb.getEntryTimeFrom() != null) {
            sb.append(" and entrytime >= ?");
            args.add(isb.getEntryTimeFrom());
        }
        if (isb.getEntryTimeTo() != null) {
            sb.append(" and entrytime < ?");
            args.add(isb.getEntryTimeTo());
        }
        countSql += " where 1=1 " + sb.toString();
        //记录总数
        Integer count = jdbcTemplate.queryForObject(countSql, Integer.class, args.toArray());
        pi.setTotal(count);

        //查询库存记录
        String sql = "select id,warehouse_id,commodity_id,inventory,entrytime from t_inventory";
        sql += " where 1 = 1" + sb.toString() + " limit ?,?";
        args.add(pi.getOffset());
        args.add(pi.getLimit());
        return jdbcTemplate.query(sql, MAPPER, args.toArray());
    }

    @Override
    public int add(Inventory inventory1) {
        String sql = "insert into t_inventory(warehouse_id,commodity_id,inventory,entrytime) values(?,?,?,?)";
        ;
        return jdbcTemplate.update(sql, inventory1.getWarehouseId(), inventory1.getCommodityId(), inventory1.getInventory(), inventory1.getEntryTime());
    }

    @Override
    public int delete(Integer[] intIds) {
        String sql = "delete from t_inventory where id in (";
        for (int i = 0; i < intIds.length; i++) {
            if (i == intIds.length - 1) {
                sql += "?";
            } else {
                sql += "?,";
            }
        }
        sql += ")";
        return jdbcTemplate.update(sql,intIds);
    }

    @Override
    public Inventory findById(Integer id) {
        String sql = "select id,warehouse_id,commodity_id,inventory,entrytime from t_inventory where id = ?";
        List<Inventory> inventoryList = jdbcTemplate.query(sql,MAPPER,id);
        return inventoryList.isEmpty()?null:inventoryList.get(0);
    }

    @Override
    public int update(Inventory inventory1) {
        String sql = "update t_inventory set warehouse_id = ?,commodity_id = ?,inventory = ?,entrytime = ? where id = ?";
        return jdbcTemplate.update(sql,inventory1.getWarehouseId(),inventory1.getCommodityId(),inventory1.getInventory(),inventory1.getEntryTime(),inventory1.getId());
    }

    @Override
    public List<Inventory> findAllWareHouseId() {
        String sql = "select distinct warehouse_id from t_inventory";
        List<Inventory> inventoryList = jdbcTemplate.query(sql,MAPPER);
//        System.out.println(inventoryList);
        return jdbcTemplate.query(sql,MAPPER);
    }

    @Override
    public List<Inventory> findByCommodityIds(String[] commodityIdsFromCommodityList, PaginateInfo pi) {
        List<Object> args = new ArrayList<>();
        String countSql = "select count(0) from t_inventory where commodity_id in(";
        String sql = "select * from t_inventory where commodity_id in(";
        for (int i = 0; i < commodityIdsFromCommodityList.length; i++) {
            if (i==commodityIdsFromCommodityList.length-1){
                countSql += "?";
                sql += "?";
            }else {
                countSql += "?,";
                sql += "?,";
            }
        }
        for (int i = 0; i < commodityIdsFromCommodityList.length; i++) {
            args.add(commodityIdsFromCommodityList[i]);
        }

        countSql += ")";
        sql+= ") order by commodity_id asc limit ?,?";
        int total = jdbcTemplate.queryForObject(countSql,Integer.class,commodityIdsFromCommodityList);
        pi.setTotal(total);
        args.add(pi.getOffset());
        args.add(pi.getLimit());
        List<Inventory> inventoryList = jdbcTemplate.query(sql,MAPPER,args.toArray());
        return inventoryList.isEmpty()?null:inventoryList;
    }

    @Override
    public List<Inventory> findByWarehouseId(String warehouseId1,PaginateInfo pi) {
        String countSql = "select count(0) from t_inventory where warehouse_id = ?";
        String sql = "select * from t_inventory where warehouse_id = ?";

        int total = jdbcTemplate.queryForObject(countSql,Integer.class,warehouseId1);
        pi.setTotal(total);
        sql+=" limit ?,?";

        return jdbcTemplate.query(sql,MAPPER,warehouseId1,pi.getOffset(),pi.getLimit());
    }

    @Override
    public List<Inventory> findByCommodityId(String commodityId) {
        String sql = "select * from t_inventory where commodity_id=?";
        List<Inventory> inventoryList = jdbcTemplate.query(sql,MAPPER,commodityId);
        System.out.println(inventoryList);
        return inventoryList.isEmpty()?null:inventoryList;
    }
}
